<?php

class Model_Article extends Model_Database
{
	var $table = 'article';
	var $table_tagmap = 'tagmap';
	public function check_param($data){
		$title_len = UTF8::strlen($data['title']);
		if ($title_len < 1 || $title_len > 50){
			return '标题错误';
		}
		if ($data['category'] == 0){
			return '类型错误';
		}
		$author_len = UTF8::strlen($data['author']);
		if ($author_len < 1 || $author_len > 20){
			return '作者错误';
		}
		$summary_len = UTF8::strlen($data['summary']);
		if ($summary_len < 1 || $summary_len > 200){
			return '摘要错误';
		}
		$content_len = UTF8::strlen($data['content']);
		if ($content_len < 1){
			return '内容错误';
		}
		
		return true;
	}
	
	public function get_list($offset = 0, $page_size = 10, $cond = array()){
		$where = 'where del=0';
		$params = array();
		foreach ($cond as $key => $param){
			$where .= " and {$key}=:{$key}";
			$params[":{$key}"] = $param;
		}
		$sql = 'select aid, title, category, author, summary, content, quote_from, tags, `read`, ';
		$sql .= '`comment`, `like`, `share`, collect, addtime, uptime, `status` ';
		$sql .= "from {$this->table} {$where} order by aid desc limit {$offset}, {$page_size}";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters($params);
		return $query->execute()->as_array();
	}
	
	public function get_count($cond = array()){
		$where = 'where del=0';
		$params = array();
		foreach ($cond as $key => $param){
			$where .= " and {$key}=:{$key}";
			$params[":{$key}"] = $param;
		}
		$sql = "select aid from {$this->table} {$where}";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters($params);
		return $query->execute()->count();
	}
	
	public function get_by_tagid($tagid, $offset = 0, $page_size = 10, $cond = array()){
		$where = 'where m.del=0 and a.del=0 and m.tagid=:tagid';
		$params = array();
		$params[":tagid"] = $tagid;
		foreach ($cond as $key => $param){
			$where .= " and {$key}=:{$key}";
			$params[":{$key}"] = $param;
		}
		$sql = 'select a.aid, a.title, a.category, a.author, a.summary, a.content, a.quote_from, a.tags, a.`read`, ';
		$sql .= 'a.`comment`, a.`like`, a.`share`, a.collect, a.addtime, a.uptime, a.`status` ';
		$sql .= "from {$this->table_tagmap} as m ";
		$sql .= "left join {$this->table} as a on a.aid=m.aid {$where} order by a.aid desc limit {$offset}, {$page_size}";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters($params);
		return $query->execute()->as_array();
	}
	
	public function get_count_by_tagid($tagid, $cond = array()){
		$where = 'where m.del=0 and a.del=0 and m.tagid=:tagid';
		$params = array();
		$params[":tagid"] = $tagid;
		foreach ($cond as $key => $param){
			$where .= " and {$key}=:{$key}";
			$params[":{$key}"] = $param;
		}
		$sql = "select a.aid from {$this->table_tagmap} as m left join {$this->table} as a on a.aid=m.aid {$where}";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters($params);
		return $query->execute()->count();
	}
	
	public function get_by_aid($aid, $cond = array()){
		$cond['aid'] = $aid;
		$re = $this->get_list(0, 1, $cond);
    	return isset($re[0]) ? $re[0] : false;
	}
	
	public function get_hot_one_week($num = 10){
		$begin = date("Y-m-d",strtotime("-1 week")) . ' 00:00:00';
		$sql = 'select aid, title, category, author, summary, content, quote_from, tags, `read`, ';
		$sql .= '`comment`, `like`, `share`, collect, addtime, uptime, `status` ';
		$sql .= "from {$this->table} where del=0 and status=0 and addtime >= :begin order by aid desc";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':begin' => $begin,
		));
		$re = $query->execute()->as_array();
		$rank = array();
		foreach ($re as $article){
			$rank[] = $article['like'] * 0.3 + $article['share'] * 0.2 + $article['collect'] * 0.2 + $article['comment'] * 0.2 + $article['read'] * 0.1;
		}
		array_multisort($rank, SORT_DESC, SORT_NUMERIC, $re, SORT_DESC);
		return array_slice($re, 0, $num);
	}
	
	public function add($data){
		$title = addslashes(trim($data['title']));
		$category = intval($data['category']);
		$summary = addslashes(trim($data['summary']));
		$content = addslashes(trim($data['content']));
		$author = addslashes(trim($data['author']));
		$quote_from = addslashes(trim($data['quote_from']));
		$tags = addslashes(trim($data['tags']));
		$addtime = date('Y-m-d H:i:s');
		
		$sql = "insert into {$this->table} (title, category, summary, content, author, quote_from, tags, addtime, status) ";
		$sql .= "values('{$title}', {$category}, '{$summary}', '{$content}', '{$author}', '{$quote_from}', '{$tags}', '{$addtime}', 2)";
		$re = $this->_db->query(Database::INSERT, $sql); // array(0=>'id', 1=>'结果')
		if (isset($re[1]) && $re[1] == 1){
			return $re[0];
		}else {
			return false;
		}
	}
	
	public function plus_one($cols, $aid){
		$ups = array();
		foreach ($cols as $property){
			$ups[] = "{$property}={$property}+1";
		}
		$sql = "update {$this->table} set " . implode(',', $ups) . " where aid={$aid}";
		return $this->_db->query(Database::UPDATE, $sql);
	}
	
	public function update($data, $aid){
		$ups = array();
		foreach ($data as $key => $property){
			$property = addslashes(trim($property));
			$ups[] = "{$key}='{$property}'";
		}
		$sql = "update {$this->table} set " . implode(',', $ups) . " where aid={$aid}";
		return $this->_db->query(Database::UPDATE, $sql);
	}
	
	public function up_status($status, $aid){
		$aids = is_array($aid) ? implode(',', $aid) : $aid;
		$sql = "update {$this->table} set status={$status} where aid in ({$aids})";
		return $this->_db->query(Database::UPDATE, $sql);
	}
	
	public function del($aid){
		$aids = is_array($aid) ? implode(',', $aid) : $aid;
		$sql = "update {$this->table} set del=1 where aid in ({$aids})";
		return $this->_db->query(Database::UPDATE, $sql);
	}
	
	public function get_list_page_admin($page = 1, $page_size = 10){
		$offset = ($page - 1) * $page_size;
		$re = $this->get_list($offset, $page_size);
		$total = $this->get_count();
		$page_num = ceil($total / $page_size);
		$page_html = '<span style="padding-right: 20px;">共' . $total . '篇</span>';
		if ($page_num == 1){
			$page_html .= '<a href="javascript:void(0)" class="number current">1</a>';
		}elseif ($page_num <= 4){
			$page_prev = $page - 1;
			$page_next = $page + 1;
			$page_html .= '<a href="/ae/article/index/1" title="第一页">&laquo; 第一页</a>';
			$page_html .= '<a href="/ae/article/index/' . $page_prev . '" title="上一页">&laquo; 上一页</a>';
			for ($_page = 1; $_page <= $page_num; $_page++){
				if ($_page == $page){
					$page_html .= '<a href="/ae/article/index/' . $_page . '" class="number current" title="' . $_page . '">' . $_page . '</a>';
				}else {
					$page_html .= '<a href="/ae/article/index/' . $_page . '" class="number" title="' . $_page . '">' . $_page . '</a>';
				}
			}
			$page_html .= '<a href="/ae/article/index/' . $page_next . '" title="下一页">下一页 &raquo;</a>';
			$page_html .= '<a href="/ae/article/index/' . $page_num . '" title="最后一页">最后一页 &raquo;</a>';
		}else {
			$page_prev = $page - 1;
			$page_next = $page + 1;
			$page_html .= '<a href="/ae/article/index/1" title="第一页">&laquo; 第一页</a>';
			$page_html .= '<a href="/ae/article/index/' . $page_prev . '" title="上一页">&laquo; 上一页</a>';
			if ($page_prev != 0){
				$page_html .= '<span>...<span>';
				$page_html .= '<a href="/ae/article/index/' . $page_prev . '" class="number" title="' . $page_prev . '">' . $page_prev . '</a>';
			}
			$page_html .= '<a href="/ae/article/index/' . $page . '" class="number current" title="' . $page . '">' . $page . '</a>';
			if ($page_next != $page_num + 1){
				$page_html .= '<a href="/ae/article/index/' . $page_next . '" class="number" title="' . $page_next . '">' . $page_next . '</a>';
				$page_html .= '<span>...<span>';
			}
			$page_html .= '<a href="/ae/article/index/' . $page_next . '" title="下一页">下一页 &raquo;</a>';
			$page_html .= '<a href="/ae/article/index/' . $page_num . '" title="最后一页">最后一页 &raquo;</a>';
		}
		
		$result['result'] = $re;
		$result['page'] = $page_html;
		return $result;
	}
	
}
